﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NoteObject : MonoBehaviour
{
    public bool canBePressed;
    public KeyCode keyToPress;
    public GameObject hitEffect;
    public GameObject goodEffect;
    public GameObject perfectEffect;
    public GameObject missEffect;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(keyToPress))
        {
            if (canBePressed)
            {
                gameObject.SetActive(false);
                //GameManager.Instance.NoteHit();
                float distance = Mathf.Abs(transform.position.y);
                if (distance > 0.25)
                {
                    Debug.Log("Hit");
                    GameManager.Instance.NormalHit();
                    InstantiateEffect(hitEffect);
                }
                else if(distance > 0.05)
                {
                    Debug.Log("Good");
                    GameManager.Instance.GoodHit();
                    InstantiateEffect(goodEffect);
                }
                else
                {
                    Debug.Log("Perfect");
                    GameManager.Instance.PerfectHit();
                    InstantiateEffect(perfectEffect);
                }
            }
        }
    }

    void InstantiateEffect(GameObject effect)
    {
        Instantiate(effect, transform.position, effect.transform.rotation);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "Activator")
        {
            canBePressed = true;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.tag == "Activator")
        {
            if (Input.GetKeyDown(keyToPress))
            {
                canBePressed = true;
            }
            else
            {
                canBePressed = false;
                GameManager.Instance.NoteMissed();
                InstantiateEffect(missEffect);
            }
        }
    }
}
